package com.tevintao.marketingsystem.tool;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Random;

/**
 * Created by tevintao on 2016/3/24.
 */
public class PhoneTool {

    private static TelephonyManager telephonyManager = null;

    private static TelephonyManager getTelephonyManager(Context context) {
        if (null == telephonyManager) {
            synchronized (PhoneTool.class) {
                if (null == context) {
                    return null;
                } else {
                    telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                }
            }
        }
        return telephonyManager;
    }

    public static String getMethodValue(Context context, String methodName, String params1, Class<?>... parameterTypes) {
        TelephonyManager tm = getTelephonyManager(context);
        if (null == tm) {
            return null;
        }

        Class pmCls = TelephonyManager.class;
        try {
            Method method = null;

            if (null == parameterTypes) {
                method = pmCls.getDeclaredMethod(methodName);
            } else {
                method = pmCls.getDeclaredMethod(methodName, parameterTypes);
            }
            if (null == method) {
                return null;
            }
            method.setAccessible(true);
            if (TextUtils.isEmpty(params1)) {
                return method.invoke(tm).toString();
            } else {
                return method.invoke(tm, params1).toString();
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean getMobileNetIsAvailable(Context context) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取代表联网状态的NetWorkInfo对象
        NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
        // 获取当前的网络连接是否可用
        return networkInfo.isAvailable();
    }

    public static final int CHINA_MOBILE = 0;
    public static final int CHINA_UNICOM = 1;
    public static final int CHINA_TELECOM = 3;
    public static final int MOBILE_TYPE_ERROR = 4;

    public static String getMobileTypeName(Context context) {
        TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = telManager.getSimOperator();
        if (operator != null) {
            if (operator.equals("46000") || operator.equals("46002") || operator.equals("46007")) {
                return "中国移动";
            } else if (operator.equals("46001") || operator.equals("46009")) {
                return "中国联通";
            } else if (operator.equals("46003")) {
                return "中国电信";
            }
        }
        return "";
    }

    public static String getMac(Context context) {
        WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = wifi.getConnectionInfo();
        return info.getMacAddress();
    }

    public static int getMobileTypeInt(Context context) {
        TelephonyManager telManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String operator = telManager.getSimOperator();
//        LogManager.getInstance().logInfo("getMobileTypeInt = " + operator);
        if (operator != null) {
            if (operator.equals("46000") || operator.equals("46002") || operator.equals("46007")) {
                return CHINA_MOBILE;
            } else if (operator.equals("46001") || operator.equals("46009")) {
                return CHINA_UNICOM;
            } else if (operator.equals("46003")) {
                return CHINA_TELECOM;
            }
        }
        return -1;
    }

    private static Random r = new Random();

    public static String getRandomMac(String trueMac) {
        if (TextUtils.isEmpty(trueMac)) {
            return null;
        }

        String[] values = trueMac.split(":");
        if (values.length != 6) {
            return null;
        }
        int v1 = r.nextInt(256);
        values[3] = Integer.toHexString(0x100 | v1).substring(1);

        int v2 = r.nextInt(256);
        values[4] = Integer.toHexString(0x100 | v2).substring(1);

        int v3 = r.nextInt(256);
        values[5] = Integer.toHexString(0x100 | v3).substring(1);


        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < values.length; i++) {
            sb.append(values[i]);
            if (i != values.length - 1) {
                sb.append(":");
            }
        }
        return sb.toString();
    }

    public static String getRandomImei() {
        return getRandomImei("86");
    }

    public static String getRandomImei(String quhao) {
        if (null == quhao) {
            quhao = "";
        }
        String imei = quhao;
        while (imei.length() < 14)
            imei += Character.forDigit(r.nextInt(10), 10);
        imei += getLuhnDigit(imei);
        return imei;
    }

    private static char getLuhnDigit(String x) {
        // http://en.wikipedia.org/wiki/Luhn_algorithm
        int sum = 0;
        for (int i = 0; i < x.length(); i++) {
            int n = Character.digit(x.charAt(x.length() - 1 - i), 10);
            if (i % 2 == 0) {
                n *= 2;
                if (n > 9)
                    n -= 9; // n = (n % 10) + 1;
            }
            sum += n;
        }
        return Character.forDigit((sum * 9) % 10, 10);
    }

}
